package com.hwq.sql.script.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwq.sql.script.entity.User;
import com.hwq.sql.script.mapper.UserMapper;
import com.hwq.sql.script.model.po.UserPO;
import com.hwq.sql.script.model.vo.UserVO;
import com.hwq.sql.script.utils.StrUtil;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    /**
     * 根据 名称 查询
     * @param name 名称
     */
    public User getByName(String name) {
        LambdaQueryWrapper<User> wrapper = new QueryWrapper<User>().lambda();
        wrapper.eq(User::getName, name);
        return getOne(wrapper);
    }

    /**
     * 根据 账号 查询
     * @param account 账号
     */
    public User getByAccount(String account) {
        LambdaQueryWrapper<User> wrapper = new QueryWrapper<User>().lambda();
        wrapper.eq(User::getAccount, account);
        return getOne(wrapper);
    }

    /**
     * 分页条件查询
     * @param po 查询条件
     */
    public IPage<UserVO> page(UserPO po) {
        IPage<UserVO> iPage = new Page<>(po.getCurrent(), po.getSize());
        String name = StrUtil.isBlank(po.getName()) ? null : "%" + po.getName() + "%";
        String account = StrUtil.isBlank(po.getAccount()) ? null : "%" + po.getAccount() + "%";
        iPage = baseMapper.page(iPage, name, account);
        return iPage;
    }

}
